package com.school.vaccination.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.vaccination.domain.Child;
import com.school.vaccination.domain.Record;
import com.school.vaccination.domain.Vaccine;
import com.school.vaccination.mapper.RecordMapper;
import com.school.vaccination.mapper.VaccineMapper;
import com.school.vaccination.model.ChildWaitModel;
import com.school.vaccination.model.RecordModel;
import com.school.vaccination.service.RecordService;
import zhibi.common.support.base.Page;
import zhibi.common.support.base.service.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.util.IDUtil;

import java.util.Date;

/**
* The class create by 执笔 on generate.
*/
@Service
@Transactional
public class RecordServiceImpl extends BaseServiceImpl<Record> implements RecordService {

    @Autowired
    public RecordServiceImpl(RecordMapper mapper) {
        super(mapper);
        this.recordMapper = mapper;
    }
    private RecordMapper recordMapper;
    @Autowired
    private VaccineMapper vaccineMapper;

    @Override
    public PageInfo<RecordModel> selectModel(Example example, Page page) {
        if(null != page){
            PageHelper.startPage(page.getPageNum(),page.getPageSize());
        }
        return new PageInfo<>(recordMapper.selectModel(example),Page.NAV_SIZE);
    }

    @Override
    public PageInfo<ChildWaitModel> selectWaitModel(Example example, Page page) {
        if(null != page){
            PageHelper.startPage(page.getPageNum(),page.getPageSize());
        }
        return new PageInfo<>(recordMapper.selectWaitModel(example),Page.NAV_SIZE);
    }

    @Override
    public void invince(Child child,Vaccine vaccine) {
        vaccine.setCount(vaccine.getCount() - 1);
        vaccineMapper.updateByPrimaryKeySelective(vaccine);
        Record record = new Record();
        record.setAddtime(new Date());
        record.setOrderno(IDUtil.getUUID());
        record.setVaccineId(vaccine.getId());
        record.setChildId(child.getId());
        recordMapper.insertSelective(record);
    }
}